#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
//首先保证自己选择的数据结构(链表)的实现没问题
struct student 
{
	char name[20];
	char num[20];
	int math;
	int english;
	int py;
	int sum;			//总分
	int average;		//平均分
};
//所有关于数据的地方都要修改
struct Node
{
	struct student  data;
	struct Node* next;
};
struct Node* list;
//创建表头-->创建结构体变量
struct Node* createHead()
{
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//	assert(headNode);			//断言
	headNode->next = NULL;
	return headNode;
}
struct Node* createNode(struct student data)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
//	assert(newNode);
	//->
	newNode->data = data;
	(*newNode).next = NULL;
	return newNode;
}
void insertData(struct Node* headNode, struct student data)
{
	struct Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}
void printList(struct Node* headNode) 
{
	struct Node* pmove = headNode->next;
	printf("姓名\t编号\t数学\t英语\t物理\t总分\t平均分\n");
	while (pmove != NULL) 
	{
		printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\n", pmove->data.name,
			pmove->data.num, pmove->data.math, pmove->data.english, pmove->data.py, 
			pmove->data.sum, pmove->data.average);
		pmove = pmove->next;
	}
}
void deleteDataByName(struct Node* headNode, const char* name)
{
	struct Node* preNode = headNode;
	struct Node* curNode = headNode->next;
	while (curNode != NULL && strcmp(curNode->data.name,name)) 
	{
		preNode = curNode;
		curNode = preNode->next;
	}
	if (curNode==NULL) 
	{
		printf("删除失败!,没有找到指定数据！\n");
	}
	else 
	{
		preNode->next = curNode->next;
		free(curNode);
	}
}
struct Node* searchDataByName(struct Node* headNode, const char* name) 
{
	struct Node* pmove = headNode->next;
	while (pmove != NULL && strcmp(pmove->data.name,name)) 
	{
		pmove = pmove->next;
	}
	return pmove;
}
//管理系统文件操作
//写文件
void saveToFile(struct Node* headNode, const char* fileName) //const char*是指向可更改char的常量指针 
{
	struct Node* pmove = headNode->next;
	FILE* fp = fopen(fileName, "w");
	while (pmove != NULL) 
	{
		fprintf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%d\n", pmove->data.name,
			pmove->data.num, pmove->data.math, pmove->data.english, pmove->data.py,
			pmove->data.sum, pmove->data.average);
		pmove = pmove->next;
	}
	fclose(fp);
}
//读文件
void readFromFile(struct Node* headNode, const char* fileName) 
{
	FILE* fp = fopen(fileName, "r");
	if (fp == NULL) 
	{
		fp = fopen(fileName, "w+");
		fclose(fp);
	}
	struct student temp;
	while (fscanf(fp, "%s\t%s\t%d\t%d\t%d\t%d\t%d\n",
		temp.name,
		temp.num,
		&temp.math,
		&temp.english,
		&temp.py,
		&temp.sum,
		&temp.average) != EOF) 
	{
		insertData(list, temp);
	}
	fclose(fp);
}

void sortMenu()
{
	printf("---------【学生排序系统】---------\n");
	printf("\t\t0.退出功能\n");
	printf("\t\t1.按英语成绩排序\n");
	printf("\t\t2.按数学成绩排序\n");
	printf("\t\t3.按物理成绩排序\n");
	printf("\t\t4.按加权成绩排序\n");
	printf("-------------------------------------\n");
	printf("请输入(0~4):");
}
void makeMenu()		  //制作菜单
{
	//排序+文件
	printf("---------【链式学生管理系统】---------\n");
	printf("\t\t0.退出功能\n");
	printf("\t\t1.录入功能\n");
	printf("\t\t2.浏览功能\n");
	printf("\t\t3.查找功能\n");
	printf("\t\t4.修改功能\n");
	printf("\t\t5.删除功能\n");
	printf("\t\t6.排序功能\n");
	printf("-------------------------------------\n");
	printf("请输入(0~6):");
}
void swapData(struct Node* a, struct Node* b) {
    student temp = a->data;
    a->data = b->data;
    b->data = temp;
}
void sortList_english_small(struct Node* headNode) {//a为想要排序的科目 
    if (headNode == NULL)
        return;

    int swapped;
    struct Node* current;
    struct Node* last = NULL;
    do {
        swapped = 0;
        current = headNode->next;
        while (current->next != last) {
            if (current->data.english > current->next->data.english) {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;
        }
        last = current;
    } while (swapped);
}
void sortList_english_big(struct Node* headNode) {//a为想要排序的科目 
    if (headNode == NULL)
        return;

    int swapped;
    struct Node* current;
    struct Node* last = NULL;
    do {
        swapped = 0;
        current = headNode->next;
        while (current->next != last) {
            if (current->data.english < current->next->data.english) {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;
        }
        last = current;
    } while (swapped);
}
void sortList_math_small(struct Node* headNode) {//a为想要排序的科目 
    if (headNode == NULL)
        return;

    int swapped;
    struct Node* current;
    struct Node* last = NULL;
    do {
        swapped = 0;
        current = headNode->next;
        while (current->next != last) {
            if (current->data.math > current->next->data.math) {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;
        }
        last = current;
    } while (swapped);
}
void sortList_math_big(struct Node* headNode) {//a为想要排序的科目 
    if (headNode == NULL)
        return;

    int swapped;
    struct Node* current;
    struct Node* last = NULL;
    do {
        swapped = 0;
        current = headNode->next;
        while (current->next != last) {
            if (current->data.math < current->next->data.math) {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;
        }
        last = current;
    } while (swapped);
}
void sortList_py_small(struct Node* headNode) { 
    if (headNode == NULL)
        return;

    int swapped;
    struct Node* current;
    struct Node* last = NULL;
    do {
        swapped = 0;
        current = headNode->next;
        while (current->next != last) {
            if (current->data.py > current->next->data.py) {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;
        }
        last = current;
    } while (swapped);
}
void sortList_py_big(struct Node* headNode) { 
    if (headNode == NULL)
        return;

    int swapped;
    struct Node* current;
    struct Node* last = NULL;
    do {
        swapped = 0;
        current = headNode->next;
        while (current->next != last) {
            if (current->data.py < current->next->data.py) {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;
        }
        last = current;
    } while (swapped);
}
void keyDown2()
{
	int userkey1 = 0;
	struct student temp;			//接受用户的输入
	struct Node* result;
	scanf("%d", &userkey1);
	switch (userkey1) 
	{
	case 0:
		printf("----【退出模块】----\n");
		system("pause");
		exit(0);
		break;

	case 1:
		system("cls");
	//	printf("----【按英语成绩排序】----\n");
		printf("----【按从小到大的顺序排序】----\n");
        sortList_english_small(list);
        printList(list);
		break;
	case 2:
		system("cls");
	//	printf("----【按英语成绩排序】----\n");
		printf("----【按从大到小的顺序排序】----\n");
        sortList_english_big(list);
        printList(list);
		break;
					
	default:
		printf("输入错误!,请重新输入...!\n");
		break;
	}
}
void keyDown3()
{
	int userkey1 = 0;
	struct student temp;			//接受用户的输入
	struct Node* result;
	scanf("%d", &userkey1);
	switch (userkey1) 
	{
	case 0:
		printf("----【退出模块】----\n");
		system("pause");
		exit(0);
		break;

	case 1:
		system("cls");
	//	printf("----【按英语成绩排序】----\n");
		printf("----【按从小到大的顺序排序】----\n");
        sortList_math_small(list);
        printList(list);
		break;
	case 2:
		system("cls");
	//	printf("----【按英语成绩排序】----\n");
		printf("----【按从大到小的顺序排序】----\n");
        sortList_math_big(list);
        printList(list);
		break;
					
	default:
		printf("输入错误!,请重新输入...!\n");
		break;
	}
}
void keyDown4()
{
	int userkey1 = 0;
	struct student temp;			//接受用户的输入
	struct Node* result;
	scanf("%d", &userkey1);
	switch (userkey1) 
	{
	case 0:
		printf("----【退出模块】----\n");
		system("pause");
		exit(0);
		break;

	case 1:
		system("cls");
	//	printf("----【按英语成绩排序】----\n");
		printf("----【按从小到大的顺序排序】----\n");
        sortList_py_small(list);
        printList(list);
		break;
	case 2:
		system("cls");
	//	printf("----【按英语成绩排序】----\n");
		printf("----【按从大到小的顺序排序】----\n");
        sortList_py_big(list);
        printList(list);
		break;
					
	default:
		printf("输入错误!,请重新输入...!\n");
		break;
	}
}
void keyDown1()
{
	int userkey1 = 0;
	struct student temp;			//接受用户的输入
	struct Node* result;
	scanf("%d", &userkey1);
	switch (userkey1) 
	{
	case 0:
		printf("----【退出模块】----\n");
		system("pause");
		exit(0);
		break;

	case 1:
		system("cls");
		printf("----【按英语成绩排序】----\n");
		printf("\t\t1.按从大到小的顺序\n");
	    printf("\t\t2.按从小到大的顺序\n");
	    printf("-------------------------------------\n");
	    printf("请输入(1-2):");
	    keyDown2() ;
		//system("pause");
	//	system("cls");
              
		break;
	case 2:
		system("cls");
		printf("----【按数学成绩排序】----\n");
		printf("\t\t1.按从大到小的顺序\n");
	    printf("\t\t2.按从小到大的顺序\n");
	    printf("-------------------------------------\n");
	    printf("请输入(1-2):");
	    keyDown3() ;
		//system("pause");
	//	system("cls");
		break;
	case 3:
		system("cls");
		printf("----【按物理成绩排序】----\n");
		printf("\t\t1.按从大到小的顺序\n");
	    printf("\t\t2.按从小到大的顺序\n");
	    printf("-------------------------------------\n");
	    printf("请输入(1-2):");
	    keyDown4() ;
	//	system("pause");
	//	system("cls");
		break;
	case 4:
		system("cls");
		printf("----【按加权成绩排序】----\n");
		printf("\t\t1.按从大到小的顺序\n");
	    printf("\t\t2.按从小到大的顺序\n");
	    printf("-------------------------------------\n");
	    printf("请输入(1-2):");
	    keyDown2() ;
		//system("pause");
	//	system("cls");
		break;				
//	default:
//		printf("输入错误!,请重新输入...!\n");
//		break;
	}
}

void keyDown() 
{
	int userkey = 0;
	struct student temp;			//接受用户的输入
	struct Node* result;
	scanf("%d", &userkey);
	switch (userkey) 
	{
	case 0:
		printf("----【退出模块】----\n");
		system("pause");
		exit(0);
		break;
	case 1:
		printf("----【录入模块】----\n");
		printf("请输入学生信息(name,num,math,english,py):");
		scanf("%s%s%d%d%d", temp.name,  temp.num,  &temp.math, &temp.english, &temp.py);
		//手动处理:
		temp.sum = temp.math + temp.english + temp.py;
		temp.average = temp.sum / 3;
		insertData(list, temp);
		saveToFile(list, "student.txt");
		break;
	case 2:
		printf("----【浏览功能】----\n");
		printList(list);
		break;
	case 3:
		printf("----【查找功能】----\n");
		printf("请输入要查找学生姓名:");
		scanf("%s", temp.name);  //vs 增强版的函数，20 代表长度
		result = searchDataByName(list, temp.name);
		if (result == NULL) 
		{
			printf("未找到结果！\n");
		}
		else 
		{
			printf("姓名\t编号\t数学\t英语\t物理\t总分\t平均分\n");
			printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\n",
				result->data.name,
				result->data.num, 
				result->data.math, 
				result->data.english, 
				result->data.py,
				result->data.sum, 
				result->data.average);
		}
		break;
	case 4:
		printf("----【修改功能】----\n");
		printf("请输入要修改学生姓名:");
		scanf("%s", temp.name);  //vs 增强版的函数，20 代表长度
		result = searchDataByName(list, temp.name);
		if (result == NULL)
		{
			printf("未找到结果！\n");
		}
		else
		{
			printf("请输入学生新的信息(姓名,编号,数学,英语,物理):");
			scanf("%s%s%d%d%d", result->data.name,  result->data.num,  &result->data.math, &result->data.english, &result->data.py);
			//手动处理:
			result->data.sum = result->data.math + result->data.english + result->data.py;
			result->data.average = result->data.sum / 3;
			saveToFile(list, "student.txt");
		}
		break;
	case 5:
		printf("----【删除功能】----\n");
		printf("请输入要删除学生姓名:");
		scanf("%s", temp.name);  //vs 增强版的函数，20 代表长度
		deleteDataByName(list, temp.name);
		saveToFile(list, "student.txt");
		break;
	case 6:
		system("cls");
		printf("------【排序功能】-----\n");
		sortMenu();
		keyDown1();
		//system("pause");		//按任意键继续
	//	system("cls");
	    break;
	default:
		printf("输入错误!,请重新输入...!\n");
		break;
	}
}
 
int main() 
{
	list = createHead();
	readFromFile(list, "student.txt");
	while (1) 
	{
		makeMenu();
		keyDown();
		system("pause");		//按任意键继续
		system("cls");			//清屏
	}
	return 0;
}

